package com.qf.controller;

import com.qf.pojo.DtsComment;
import com.qf.service.DtsAdminDataAuthService;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.AuthSupport;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangYuJie
 * projectName:shop_manage
 * time:2022/5/18 7:36
 * description:
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/admin/comment")
public class DtsAdminCommentController {
    @Autowired
    DtsAdminDataAuthService adminDataAuthService;

    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsCommentService commentService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsComment comment){
        commentService.deleteById(comment.getId());
        return ResponseUtil.ok();
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(required = false, defaultValue = "1") Integer page ,
                       @RequestParam(required = false, defaultValue = "20") Integer limit,
                       @RequestParam(required = false, defaultValue = "add_time") String sort ,
                       @RequestParam(required = false, defaultValue = "desc") String order,
                       String userId, String valueId){
        //判断是否是品牌管理员
        boolean flag = adminDataAuthService.isBrandManager(AuthSupport.currentUser());
        //如果是品牌管理员，则查询该用户的品牌 商铺
        List<Integer> brandIds = null;
        if(flag){
            brandIds = adminDataAuthService.getBrandIds(AuthSupport.currentUser());
            if(brandIds == null || brandIds.size() == 0){
                Map<String,Object> data = new HashMap<>();
                data.put("total", 0);
                data.put("items", null);

                return ResponseUtil.ok(data);
            }
        }
        Map<String,Object> data = commentService.querySelective(brandIds, userId, valueId, page, limit, sort,
                order);

        return ResponseUtil.ok(data);
    }
}
